package com.cortex.pineapple.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.DropBoxManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cortex.pineapple.R;
import com.cortex.pineapple.database.SeasonQuerySearch;
import com.cortex.pineapple.database.VisitLink;
import com.cortex.pineapple.fetch.RequestData.SOURCE_TYPE;
import com.cortex.pineapple.listener.DragDropView;
import com.cortex.pineapple.listener.DragDropView.OnStateChangedListener;
import com.cortex.pineapple.listener.SlidListener;
import com.cortex.pineapple.utilities.MessagesLog;
import com.cortex.pineapple.view.CortexTextView;
import com.cortex.pineapple.view.CortexTextView.TYPE_FACE;


public abstract class ItemListAdapter extends BaseAdapter {

	protected Context context;
	protected List<ItemList> items;
	protected List<Header> headers;
	protected LayoutInflater inflater;
	private OnDragItemListener onDragRowListener;
	protected boolean isDragMode;
	private int lastPosition = 0;

	public ItemListAdapter(Context context) {
		this.context = context;
		items = new ArrayList<ItemList>();
		headers = new ArrayList<Header>();
		inflater = LayoutInflater.from(context);
	}
	public ItemListAdapter(Context context, List<Header> seasons) {
		this(context);
		addItems(seasons);
	}
	public void addItems(List<Header> headers) {
		for (Header header : headers) {
			// add header
			items.add(header);
			// add all items of header
			items.addAll(header.getItems());
		}
		this.headers.addAll(headers);

		Log.d(MessagesLog.APPLICATION,"items = " + items.toString());
		notifyDataSetChanged();
	}
	public void setOnDragItemListener(OnDragItemListener onDragRowListener) {
		this.onDragRowListener = onDragRowListener;
	}
	private boolean isValid(){
		return items.isEmpty()?false:true;
	}
	public SeasonQuerySearch getHeaderByItem(int item){
		if(!isValid()){
			return null;
		}
		return (SeasonQuerySearch) items.get(numberOfHeaderByItem(item)).getData();
	}
	public int numberOfHeaderByItem(int position){
		if(!isValid()){
			return -1;
		}
		for (int i = position; i >= 0; i--) {
			ItemList item = items.get(i);
			if(item.isHeader()){
				return items.indexOf(item);
			}
		}
		return 0;
	}
	public boolean isHeader(int position) {
		if(position < 0){
			position = 0;
		}
		if(position >= headers.size()){
			position = headers.size() - 1;
		}
		return items.get(position).isHeader();
	}
	public SeasonQuerySearch getPreviousHeader(int row){
		int currentHeader = numberOfHeaderByItem(row);
		int previousHeader = currentHeader - 1;
		return getHeaderByItem(previousHeader);
	}
	public int numberOfHeaders(){
		return headers.size();
	}

	public int numberOfItemsInHeader(int header){
		return headers.get(header).getItems().size();
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position).getData();
	}

	@Override
	public long getItemId(int position) {
		return 0;
//		return items.get(position).getId();
	}

	public List<Header> getHeaders() {
		return headers;
	}
	public void clear() {
		items.clear();
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemList item = items.get(position);
		View v;
		if(item.isHeader()){
			v =  getHeaderView(position, convertView, parent);
		}
		else{
			v = getItemView(position, convertView, parent);
		}
		Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
//		v.startAnimation(animation);
		lastPosition = position;
		return v;
	}
	
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	@Override
	public int getItemViewType(int position) {
		ItemList item = items.get(position);
		return item.getType().ordinal();
	}

	public boolean isDragMode(){
		return isDragMode;
	}

	public abstract View getHeaderView(int position,View convertView,ViewGroup parent);
	public abstract View getItemView(int position,View convertView,ViewGroup parent);
	
	public interface OnDragItemListener{
		void startDragging(View dragView,int x,int y,Item item);
		void moveDragging(View dragView,int x,int y,Item item);
		void stopDragging(View dragView,int x,int y,Item item);
	}

}
